package com.wanmait.movies.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wanmait.movies.util.DBHelper;
import com.wanmait.movies.vo.Actor;
import com.wanmait.movies.vo.Director;

public class ActorDao {

	private ActorDao() {};
	private static ActorDao actorDao = new ActorDao();
	public static ActorDao getDao() {
		
		return actorDao;
	}
	
	public int getAllCount(){
		
		Connection conn = DBHelper.getDBHelper().getconn();
		PreparedStatement ps =null;
		ResultSet rs =null;
		
		String sql = "select count(id) as c from actor";
		try {
			ps =conn.prepareStatement(sql);
			rs=ps.executeQuery();
			rs.next();
			
			int i = rs.getInt("c");
			
			return i;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
		return 0;
   }
	//修改演员
	 public void updateActor(String name,String introduce,int id){
			
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			
			String sql = "UPDATE actor set actor_name=?, actor_introduce=? where id=? ";
			try {
				ps =conn.prepareStatement(sql);
				ps.setString(1, name);
				ps.setString(2, introduce);
				ps.setInt(3, id);
			
				ps.executeUpdate();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(null, ps, conn);
			}

	   }
	//修改演员
		 public void updateActor(String name,String introduce,String image,int id){
				
				Connection conn = DBHelper.getDBHelper().getconn();
				PreparedStatement ps =null;
				
				String sql = "UPDATE actor set actor_name=?,actor_image=?,actor_introduce=? where id=? ";
				try {
					ps =conn.prepareStatement(sql);
					ps.setString(1, name);
					ps.setString(2, image);
					ps.setString(3, introduce);
					ps.setInt(4, id);
				
					ps.executeUpdate();
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally {
					DBHelper.getDBHelper().close(null, ps, conn);
				}

		   }
		   
	//获得演员分页列表
		public ArrayList<Actor> getActorList(int i){
			ArrayList<Actor> list =new ArrayList<Actor>();
			
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			ResultSet rs =null;
			
			String sql ="select * from actor  order by id limit ?,8";
			
			try {
				ps = conn.prepareStatement(sql);
				ps.setInt(1, i*8);
				rs = ps.executeQuery();
				
				while(rs.next()) {
					Actor actor =new Actor();
					actor.setId(rs.getInt("id"));
					actor.setActorName(rs.getString("actor_name"));
					actor.setActorImage(rs.getString("actor_image"));
					
					list.add(actor);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(rs, ps, conn);
			}
			return list;
		}
	//获得演员列表
	public ArrayList<Actor> getActorList(){
		ArrayList<Actor> list =new ArrayList<Actor>();
		
		Connection conn = DBHelper.getDBHelper().getconn();
		PreparedStatement ps =null;
		ResultSet rs =null;
		
		String sql ="select * from actor order by id limit 0,8 ";
		
		try {
			ps = conn.prepareStatement(sql);
			
			rs = ps.executeQuery();
			
			while(rs.next()) {
				Actor actor =new Actor();
				actor.setId(rs.getInt("id"));
				actor.setActorName(rs.getString("actor_name"));
				actor.setActorImage(rs.getString("actor_image"));
				
				list.add(actor);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
		return list;
	}
	
	//通过姓名查找演员
	
	public ArrayList<Actor> findByName(String name){
		
		ArrayList<Actor> lists =new ArrayList<Actor>();
		Connection conn = DBHelper.getDBHelper().getconn();
		PreparedStatement ps =null;
		ResultSet rs =null;
		
		String sql = "select * from actor where actor_name like '%"+name+"%'";
		try {
			ps =conn.prepareStatement(sql);
			
			rs=ps.executeQuery();
			
			while(rs.next()) {
				Actor actor =new Actor();
				actor.setId(rs.getInt("id"));
				actor.setActorName(rs.getString("actor_name"));
				actor.setActorImage(rs.getString("actor_image"));
				
				lists.add(actor);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
		return lists;
	}

	//添加新演员
   public void addByName(String name,String image,String intro){
		
		Connection conn = DBHelper.getDBHelper().getconn();
		PreparedStatement ps =null;
		ResultSet rs =null;
		
		String sql = "insert into actor(actor_name,actor_image,actor_introduce) values(?,?,?)";
		try {
			ps =conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, image);
			ps.setString(3,intro);
			ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}

   }
   
 //通过id获得演员
   public Actor findById(int id){
		
		Connection conn = DBHelper.getDBHelper().getconn();
		PreparedStatement ps =null;
		ResultSet rs =null;
		
		String sql = "select * from actor where id=?";
		try {
			ps =conn.prepareStatement(sql);
			ps.setInt(1, id);
			
			rs=ps.executeQuery();
			if(rs.next()) {
			
			Actor a= new Actor();
			a.setActorImage(rs.getString("actor_image"));
			a.setActorName(rs.getString("actor_name"));
			a.setId(id);
			a.setActorIntroduce(rs.getString("actor_introduce"));
			return a;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
		return null;
   }
   
 //删除演员
   public void deleteById(int id){
		
		Connection conn = DBHelper.getDBHelper().getconn();
		PreparedStatement ps =null;
		
		String sql = "delete from actor where id=?";
		try {
			ps =conn.prepareStatement(sql);
			ps.setInt(1, id);
		
			ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DBHelper.getDBHelper().close(null, ps, conn);
		}

   }
   
}
